﻿Public Class Location
    Private _Exist As Boolean = False
    ReadOnly Property exist() As Boolean
        Get
            Return _Exist
        End Get
    End Property
    Private _key As String
    ReadOnly Property key() As String
        Get
            Return _key
        End Get

    End Property


    Private _city As String

    Public ReadOnly Property city() As String
        Get
            Return _city
        End Get

    End Property
    Private _state As String
    Public ReadOnly Property state() As String
        Get
            Return _state
        End Get

    End Property
    Private _country As String
    Public ReadOnly Property country() As String
        Get
            Return _country
        End Get
    End Property
    Function GetAttendeeContactInfo(ByVal attendee As Outlook.Recipient) As Outlook.ContactItem

        Dim fContacts As Outlook.Folder
        Dim nSpace As Outlook.NameSpace
        Dim attendeeContact As Outlook.ContactItem = Nothing
        Dim found As Boolean = False


        nSpace = attendee.Application.GetNamespace("MAPI")
        fContacts = nSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

        Dim i As Integer
        'For Each attendeeContact In fContacts.Items
        For i = 1 To fContacts.Items.Count


            Try

                attendeeContact = fContacts.Items(i)

                System.Diagnostics.Debug.Print(i & " *************************")
                System.Diagnostics.Debug.Print(attendee.AddressEntry.Name & ": " & attendeeContact.FullName)
                System.Diagnostics.Debug.Print(attendee.EntryID)
                System.Diagnostics.Debug.Print(attendeeContact.EntryID)
                'attendee .
                If attendeeContact.FullName = attendee.AddressEntry.Name Then
                    found = True
                    Exit For
                End If
            Catch ex As Exception

            End Try
        Next

        If found = False Then
            attendeeContact = Nothing
        End If
        'findString = String.Format("[EntryID]=""" & attendee.EntryID & """")

        'attendeeContact = nSpace.GetItemFromID(attendee.EntryID, fContacts.StoreID)

        Return attendeeContact
    End Function
    Sub New(ByVal attendee As Outlook.Recipient)
        Dim pAcc As Outlook.PropertyAccessor

        Dim exUser As Outlook.ExchangeUser
        Dim found As Boolean = False
        Dim cs As String = Nothing
        Dim attendeeContact As Outlook.ContactItem
        exUser = attendee.AddressEntry.GetExchangeUser
        If Not (exUser Is Nothing) Then
            found = False
            ' Check to see if the exchange user has an entry in the local contacts folder
            'attendeeContact = GetAttendeeContactInfo(attendee)
            'If attendeeContact Is Nothing Then

            'todo: determine if we should also use country as part of location
            pAcc = exUser.PropertyAccessor
            Try
                _country = pAcc.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A26001E")
            Catch expAcc As Exception
                _country = ""
            End Try
            _city = exUser.City
            _state = exUser.StateOrProvince
            cs = exUser.City & ", " & exUser.StateOrProvince & ", " & country
            'Else
            '    _city = attendeeContact.BusinessAddressCity
            '    _state = attendeeContact.BusinessAddressState
            '    _country = attendeeContact.BusinessAddressCountry
            '    cs = attendeeContact.BusinessAddressCity & ", " & attendeeContact.BusinessAddressState & ", " & attendeeContact.BusinessAddressCountry
            'End If
        Else


            attendeeContact = attendee.AddressEntry.GetContact()
            If attendeeContact Is Nothing Then
                cs = ""
            Else
                _city = attendeeContact.BusinessAddressCity
                _state = attendeeContact.BusinessAddressState
                _country = attendeeContact.BusinessAddressCountry
                cs = attendeeContact.BusinessAddressCity & ", " & attendeeContact.BusinessAddressState & ", " & attendeeContact.BusinessAddressCountry
            End If
            found = False

        End If
        _key = cs
        If (_city = "") And (_country = "") And (_state = "") Then
            _Exist = False
        Else
            _Exist = True
        End If
    End Sub
End Class
